package com.hzy.app.utils;

public class NamingConverterUtil {

    /**
     * 将驼峰命名转换为蛇形命名
     * 例如：camelCase 转换为 camel_case
     *
     * @param camelCase 驼峰命名的字符串
     * @return 蛇形命名的字符串
     */
    public static String camelToSnake(String camelCase) {
        // 使用正则表达式匹配大写字母，并在其前面添加下划线，然后将字符串转换为小写
        return camelCase.replaceAll("([A-Z])", "_$1").toLowerCase();
    }

    /**
     * 将蛇形命名转换为驼峰命名
     * 例如：snake_case 转换为 snakeCase
     *
     * @param snakeCase 蛇形命名的字符串
     * @return 驼峰命名的字符串
     */
    public static String snakeToCamel(String snakeCase) {
        StringBuilder camelCase = new StringBuilder();
        String[] parts = snakeCase.split("_");
        for (int i = 0; i < parts.length; i++) {
            String part = parts[i];
            if (i == 0) {
                // 第一个单词保持小写
                camelCase.append(part);
            } else {
                // 后续单词首字母大写
                camelCase.append(Character.toUpperCase(part.charAt(0))).append(part.substring(1));
            }
        }
        return camelCase.toString();
    }

    public static void main(String[] args) {
        // 测试驼峰转蛇形
        String camelCase = "userName";
        String snakeCase = camelToSnake(camelCase);
        System.out.println("Camel to Snake: " + camelCase + " -> " + snakeCase);

        // 测试蛇形转驼峰
        snakeCase = "user_name";
        camelCase = snakeToCamel(snakeCase);
        System.out.println("Snake to Camel: " + snakeCase + " -> " + camelCase);
    }
}